# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

_realname=arrow
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.14.0.9000
pkgrel=8000
pkgdesc="Apache Arrow is a cross-language development platform for in-memory data (mingw-w64)"
arch=("any")
url="https://arrow.apache.org/"
license=("Apache-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-thrift"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=("staticlibs" "strip" "!buildflags")

# For installing from a local checkout, set source_dir to . and don't include
# a "source" param below
source_dir="$APPVEYOR_BUILD_FOLDER"
# else
# source_dir=apache-${_realname}-${pkgver}

# For released version:
#source=("https://archive.apache.org/dist/arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz")
#sha256sums=("ac2a77dd9168e9892e432c474611e86ded0be6dfe15f689c948751d37f81391a")
# For github dev version:
# Append `#commit=54b1b2f688e5e84b4c664b1e12a95f93b94ab2f3` to the URL to select a revision
# source=("${source_dir}"::"git+https://github.com/apache/arrow")
# sha256sums=("SKIP")
# source_dir="${APPVEYOR_BUILD_FOLDER}/${source_dir}"

cmake_build_type=release
cpp_build_dir=build-${CARCH}-cpp

pkgver() {
  # The only purpose of this here is to cause the job to error if the
  # version in pkgver is different from what is in r/DESCRIPTION
  grep Version "${source_dir}/r/DESCRIPTION" | cut -d " " -f 2
}

build() {
  ARROW_CPP_DIR="${source_dir}/cpp"
  [[ -d ${cpp_build_dir} ]] && rm -rf ${cpp_build_dir}
  mkdir -p ${cpp_build_dir}
  pushd ${cpp_build_dir}

  # Workaround to fix static libparquet
  export CXXFLAGS="-DARROW_STATIC"

  # This is the difference between rtools-packages and rtools-backports
  # Remove this when submitting to rtools-packages
  export CC="/C/Rtools${MINGW_PREFIX/mingw/mingw_}/bin/gcc"
  export CXX="/C/Rtools${MINGW_PREFIX/mingw/mingw_}/bin/g++"
  export PATH="/C/Rtools${MINGW_PREFIX/mingw/mingw_}/bin:$PATH"
  export CPPFLAGS="-I${MINGW_PREFIX}/include"
  export LIBS="-L${MINGW_PREFIX}/libs"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
    ${ARROW_CPP_DIR} \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${cmake_build_type} \
    -DARROW_BUILD_STATIC=ON \
    -DARROW_BUILD_SHARED=OFF \
    -DARROW_PARQUET=ON \
    -DARROW_HDFS=OFF \
    -DARROW_BOOST_USE_SHARED=OFF \
    -DARROW_WITH_ZSTD=OFF \
    -DARROW_WITH_LZ4=OFF \
    -DARROW_JEMALLOC=OFF \
    -DARROW_WITH_ZLIB=ON \
    -DARROW_WITH_BROTLI=OFF \
    -DARROW_USE_GLOG=OFF \
    -DARROW_BUILD_UTILITIES=ON \
    -DARROW_TEST_LINKAGE="static" \
    -Ddouble-conversion_ROOT="${MINGW_PREFIX}" \
    -DThrift_ROOT="${MINGW_PREFIX}"

  # Workaround for a compile error that appears with GCC 4.9.3
  sed -i 's/-fPIC/ /g' flatbuffers_ep-prefix/src/flatbuffers_ep-stamp/flatbuffers_ep-configure-RELEASE.cmake

  make
  popd
}

package() {
  make -C ${cpp_build_dir} DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  for pc in *.pc; do
    sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i $pc
  done
  popd
}
